﻿using System;
using System.Text.RegularExpressions;

namespace YG.CommonFuncs
{
    /// <summary>
    /// 字符串操作
    /// </summary>
    public static class QstringFunction
    {
        /// <summary>
        /// 找到第一个匹配字符串的之后的字符串
        /// </summary>
        /// <param name="ora"></param>
        /// <param name="val"></param>
        /// <returns>string</returns>
        /// <remark>2018年9月6日16:36:47 </remark>
        ///
        public static string SubAfter(string ora, string val)
        {
            try
            {
                int cp = ora.IndexOf(val);
                if (cp < 0)
                {
                    return null;
                }
                else
                {
                    return ora.Substring(cp + val.Length, ora.Length - (cp + val.Length));
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 找到最后一个匹配字符串的之后的字符串
        /// </summary>
        /// <param name="ora"></param>
        /// <param name="val"></param>
        /// <returns>string</returns>
        /// <remark>2018年9月6日16:36:47 </remark>
        public static string SubLastAfter(string ora, string val)
        {
            try
            {
                int cp = ora.LastIndexOf(val);
                if (cp < 0)
                {
                    return null;
                }
                else
                {
                    return ora.Substring(cp + val.Length, ora.Length - (cp + val.Length));
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 找到第一个匹配字符串前面的字符串
        /// </summary>
        /// <param name="ora"></param>
        /// <param name="val"></param>
        /// <returns>string</returns>
        /// <remark>2018年9月6日16:36:47 </remark>
        public static string SubBefore(string ora, string val)
        {
            try
            {
                int cp = ora.IndexOf(val);
                if (cp < 0)
                {
                    return null;
                }
                else
                {
                    return ora.Substring(0, cp);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 寻找最后一个匹配字符串的前面的字符串
        /// </summary>
        /// <param name="ora"></param>
        /// <param name="val"></param>
        /// <returns>string</returns>
        /// <remark>2018年9月6日16:36:47 </remark>
        public static string SubLastBefore(string ora, string val)
        {
            try
            {
                int cp = ora.LastIndexOf(val);
                if (cp < 0)
                {
                    return null;
                }
                else
                {
                    return ora.Substring(0, cp);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 使用字符串切割字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="val"></param>
        /// <returns>string数组</returns>
        /// <remark>2018年9月6日16:35:59 </remark>
        public static string[] SubSplitStrWithStr(string str, string val)
        {
            try
            {
                return Regex.Split(str, val, RegexOptions.IgnoreCase);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 使用字符串切割字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="val"></param>
        /// <returns>string数组</returns>
        /// <remark>2018年9月6日16:44:55 </remark>
        public static string[] SubSplitStrWithStrArr(string str, char[] val)
        {
            try
            {
                return str.Split(val);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 删除用户指定的某一个字符或者字符串
        /// </summary>
        /// <param name="ora"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string SubDelete(string ora, string val)
        {
            try
            {
                if (ora.Length > 0)
                {
                    return ora.Replace(val, "");
                }
                else
                {
                    return ora;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}